﻿using _250210作业.BLL;
using _250210作业.Model;
using _250210作业.UI.Models;
using MVCProject.UI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _250210作业.UI.Controllers
{
    public class UsersController : Controller
    {
        // GET: Users
        public ActionResult Index()
        {
            string page = int.Parse("250").ToString();
            return View();
        }
        public ActionResult Page(int page, int limit, string name)
        {
            UsersBLL bll = new UsersBLL();
            int count = 0;
            var list = bll.GetUsersByPage(page, limit, name, out count);
            var data = from u in list
                       select new UsersDto
                       {
                           UserID = u.userID,
                           Name = u.userName,
                           Sex = u.sex,
                           Birthday = u.birthday,
                           Phone = u.phone,
                           Pwd = u.loginPwd,
                           Status = u.status,
                           RoleID = (int)u.roleID,
                           RoleName = u.Roles.roleName,
                           DeptID = (int)u.Roles.deptID,
                           DeptName = u.Roles.Department.deptName
                       };
            ResultPageList<UsersDto> result = new ResultPageList<UsersDto>();
            result.code = 1;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            string strjson = JsonConvert.SerializeObject(result);

            return Content(strjson, "application/json");

        }
        [HttpGet]
        public ActionResult Create()
        {
            RolesBLL bll = new RolesBLL();
            var roles = bll.GetRoles();

            ViewBag.Roles = roles;

            return View();
        }
       
        [HttpGet]
        public ActionResult Edit(int id)
        {


            RolesBLL rbll = new RolesBLL();
            var roles = rbll.GetRoles();

            UsersBLL ubll = new UsersBLL();

            var user = ubll.GetUsers().Where(u => u.userID == id).FirstOrDefault();

            ViewBag.Roles = roles;
            ViewData["roles"] = roles;

            //将model 模型数据传递给 视图
            return View(user);
        }

        // DELETE: api/Users/5
        public void Delete(int id)
        {
        }

    }
}
